import Utils from "./Utils";

class Food {
    private readonly _element: HTMLElement;

    constructor() {
        this._element = document.getElementById("food")!; // !：空抑制运算符，表示断言不为空
    }

    /**
     * 重新刷新Food
     * @param xYs
     */
    respawn(xYs: number[][]) {
        let x = Math.round(Math.random() * 29) * 10;
        let y = Math.round(Math.random() * 29) * 10;
        while (Food.inxYs(x,y, xYs)) {
            x = Math.round(Math.random() * 29) * 10;
            y = Math.round(Math.random() * 29) * 10;
        }
        Utils.setX(this._element, x);
        Utils.setY(this._element, y);
    }

    /**
     * 判断当前坐标是否在xYs中
     * @param x
     * @param y
     * @param xYs
     * @returns {boolean}
     */
    private static inxYs(x: number, y: number, xYs: number[][]) {
        for (const xY of xYs) {
            if (x === xY[0] && y === xY[1]) {
                return true;
            }
        }
        return false;
    }

    get x(): number {
        return Utils.getX(this._element);
    }

    get y(): number {
        return Utils.getY(this._element);
    }

    get element(): HTMLElement {
        return this._element;
    }
}

export default Food;